home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Think Class Libraries / File class library / sources / CIsleDataFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-30  |  6.6 KB  |  268 lines  |  [TEXT/KAHL]

  1. /*
  2.  
  3.   CIsleDataFile.c
  4.   Superclass:    CIsleFile
  5.  
  6.      CDataFile transplanted into CIsleFile
  7.  
  8.   March 20, 1992  isl
  9.   
  10. */
  11.  
  12. #include    <CIsleDataFile.h>
  13.  
  14. /*=====================*/
  15. /*===---------------===*/
  16.  
  17. /******************************************************************************
  18.  CIsleDataFile.c
  19.  
  20.                             The File Class
  21.         
  22.     Class for a disk-based file. Information can be read from or written to
  23.     a File.
  24.     
  25.     SUPERCLASS = CIsleFile
  26.     
  27.     Copyright © 1989 Symantec Corporation. All rights reserved.
  28.     
  29.     TCL 1.1 CHANGES
  30.     [
  31.         -    changed implementation to use exceptions. This also changed
  32.             several interfaces because errors are now reported through
  33.             the failure mechanism instead of through function results.
  34.             If you need or prefer the old interface, use the compatibility
  35.             class ODataFile.
  36.     ]
  37.  
  38.  ******************************************************************************/
  39.  
  40. #include "CIsleDataFile.h"
  41.  
  42. /**** C O N S T R U C T I O N / D E S T R U C T I O N   M E T H O D S ****/
  43.  
  44.  
  45. /******************************************************************************
  46.  IDataFile
  47.  
  48.         Initialize a Data File object
  49.  ******************************************************************************/
  50.  
  51. void    CIsleDataFile::IDataFile()
  52. {
  53.     CIsleFile::IIsleFile();
  54.  
  55.     refNum = 0;
  56. }
  57.  
  58.  
  59. /**** A C C E S S I N G   M E T H O D S ****/
  60.  
  61.  
  62. /******************************************************************************
  63.  SetLength
  64.  
  65.         Set the length, in bytes, of the data fork of a file. File
  66.         must be already open.
  67.  ******************************************************************************/
  68.  
  69. void    CIsleDataFile::SetLength(
  70.     long        aLength)
  71. {
  72.     FailOSErr( SetEOF(refNum, aLength) );
  73. }
  74.  
  75.  
  76. /******************************************************************************
  77.  GetLength
  78.  
  79.         Retrieve the length in bytes of the data fork of a file. File
  80.         must be already open.
  81.  ******************************************************************************/
  82.  
  83. long    CIsleDataFile::GetLength( void)
  84. {
  85.     long theLength;
  86.     
  87.     FailOSErr( GetEOF( refNum, &theLength));
  88.     return theLength;
  89. }
  90.  
  91.  
  92. /******************************************************************************
  93.  SetMark
  94.  
  95.         Specify the position of the mark which determines determines the
  96.         starting point for subsequent read/write operations. Position
  97.         is specified as a distance from a certain location:
  98.             fsFromStart, fsFromLEOF, fsFromMark (the beginning, end,
  99.             or current mark).
  100.         Positive distances offset towards the end of the file, negative
  101.         ones towards the beginning. File must already be open.
  102.  ******************************************************************************/
  103.  
  104. void    CIsleDataFile::SetMark(
  105.     long        howFar,
  106.     short        fromWhere)
  107. {
  108.     FailOSErr( SetFPos(refNum, fromWhere, howFar) );
  109. }
  110.  
  111.  
  112. /******************************************************************************
  113.  GetMark
  114.  
  115.         Retrieve the position of the mark which determines determines the
  116.         starting point for subsequent read/write operations. File must
  117.         already be open.
  118.  ******************************************************************************/
  119.  
  120. long    CIsleDataFile::GetMark( void)
  121. {
  122.     long    markPos;
  123.     
  124.     FailOSErr( GetFPos(refNum, &markPos));
  125.     return markPos;
  126. }
  127.  
  128.  
  129. /**** O P E N / C L O S E   M E T H O D S ****/
  130.  
  131.  
  132. /******************************************************************************
  133.  Open
  134.  
  135.         Open the data fork of a file with the specified access permission
  136.  ******************************************************************************/
  137.  
  138. void    CIsleDataFile::Open(
  139.     SignedByte        permission)
  140. {
  141.     short newRefNum;
  142.     Boolean wasLocked;
  143.     
  144.     wasLocked = Lock( TRUE);
  145.     FailOSErr( HOpen(volNum, dirID, name, permission, &newRefNum));
  146.     Lock( wasLocked);
  147.     
  148.     refNum = newRefNum;
  149. }
  150.     
  151.  
  152. /******************************************************************************
  153.  Close {OVERRIDE}
  154.  
  155.         Close a File and make sure contents are written to disk
  156.  ******************************************************************************/
  157.  
  158. void    CIsleDataFile::Close()
  159. {
  160.     register OSErr        errCode;
  161.     short    theRefNum;
  162.     
  163.     if (refNum > 0)
  164.     {
  165.         theRefNum = refNum;
  166.         refNum = 0;            /* clear refnum before closing file, in case it fails */
  167.         FailOSErr( FSClose( theRefNum));
  168.         FailOSErr( FlushVol( NULL, volNum));
  169.     }
  170.     
  171. }
  172.  
  173.  
  174. /**** R E A D / W R I T E   M E T H O D S ****/
  175.     
  176.  
  177. /******************************************************************************
  178.  ReadAll
  179.  
  180.         Read the entire contents of a file into memory. A newly created
  181.         handle containing the data is returned. File must already be open.
  182.  ******************************************************************************/
  183.  
  184. Handle    CIsleDataFile::ReadAll( void)
  185. {
  186.     register OSErr    errCode;
  187.     long            length;                /* Length of file in bytes            */
  188.     Handle            contents = NULL;
  189.     
  190.     TRY
  191.     {
  192.         FailOSErr( GetEOF( refNum, &length));
  193.         contents = NewHandleCanFail( length);
  194.         FailNIL( contents);
  195.         FailOSErr( SetFPos( refNum, fsFromStart, 0L));
  196.         FailOSErr( FSRead( refNum, &length, *contents));        
  197.     }
  198.     CATCH
  199.     {
  200.         ForgetHandle( contents);
  201.     }
  202.     ENDTRY;
  203.     
  204.     return contents;
  205. }    
  206.  
  207.  
  208. /******************************************************************************
  209.  ReadSome
  210.  
  211.         Read the specified number of bytes from a file starting at the
  212.         current mark. Space must already have been allocated to store
  213.         the data and a pointer to this space is passed as a parameter.
  214.         The file must already be open.
  215.  ******************************************************************************/
  216.  
  217. void    CIsleDataFile::ReadSome(
  218.     Ptr            info,
  219.     long        howMuch)
  220. {
  221.     FailOSErr( FSRead(refNum, &howMuch, info) );
  222. }
  223.  
  224.  
  225. /******************************************************************************
  226.  WriteAll
  227.  
  228.         Write all the data in a handle to a file. The existing contents
  229.         of the file are replaces. File must already be open.
  230.  ******************************************************************************/
  231.  
  232. void    CIsleDataFile::WriteAll(
  233.     Handle        contents)
  234. {
  235.     register OSErr    errCode;
  236.     long            length;                /* Number of bytes to write out        */
  237.     
  238.     length = GetHandleSize(contents);
  239.                                         /* Move mark to beginning of file    */
  240.     FailOSErr( SetFPos( refNum, fsFromStart, 0L));
  241.     
  242.                                         /* Write out all the data            */
  243.     FailOSErr( FSWrite( refNum, &length, *contents));
  244.     
  245.                                         /* Set file length in case previous    */
  246.                                         /*   contents was bigger            */
  247.     FailOSErr( SetEOF( refNum, length));
  248.     
  249.                                             /* Force write to disk                */
  250.     FailOSErr( FlushVol( NULL, volNum));
  251.     
  252. }
  253.  
  254.  
  255. /******************************************************************************
  256.  WriteSome
  257.  
  258.         Write the specified number of bytes to a file starting at the
  259.         current mark. The file must already be open.
  260.  ******************************************************************************/
  261.  
  262. void    CIsleDataFile::WriteSome(
  263.     Ptr            info,
  264.     long        howMuch)
  265. {
  266.     FailOSErr( FSWrite(refNum, &howMuch, info) );
  267. }
  268.